<template>
	<view>
		<!-- #ifdef APP-PLUS -->
		<view>
			<uni-nav-bar left-icon="arrowleft" @clickLeft="back" title="地址管理" :statusBar='true' :fixed="true"></uni-nav-bar>
		</view>
		<!-- #endif -->
		<view class="addressmanage">
			<view class="addressmanage-list">
				<block v-for="item of list" :key="item.id">
					<view class="addressmanage-list-item">
						<view class="addressmanage-list-item-info">
							<view class="addressmanage-list-item-info-name">{{ item.recipientName }} {{ item.phone }}</view>
							<view class="addressmanage-list-item-info-address">{{ item.province }}{{ item.city }}{{ item.city }}{{ item.address }}</view>
						</view>
						<view class="addressmanage-list-item-handle">
							<view @click="onClickModifyAddress(item)" class="addressmanage-list-item-handle-mofidy">修改</view>
							<view @click="onClickDelAddress(item.id)" class="addressmanage-list-item-handle-del">删除</view>
						</view>
					</view>
				</block>
			</view>
			<view v-if="list !== undefined && list.length === 5" class="addressmanage-warn">我最多只能装下5个地址哦~</view>
			<view v-if="list !== undefined && list.length !== 5" class="addressmanage-addbtn">
				<button @click="onClickGotoAddressAdd" class="addressmanage-addbtn-btn">新增地址</button>
			</view>
		</view>
	</view>
</template>
<script>
	import { myMixin } from '@/utils/mixin.js';
	import { config } from '@/utils/config.js';
	import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'
	export default {
		mixins: [myMixin],
		data() {
			return {
				list: []
			};
		},
		components: {
			uniNavBar
		},
		onShow() {
			const token = uni.getStorageSync('token');
			if (!token) {
				uni.showToast({
					title: '暂无该用户凭证, 请先登录',
					icon: 'none'
				});
				return false;
			}
			this.getAddress();
		},
		methods: {
			back() {
				uni.navigateBack({})
			},
			getAddress() {
				uni.showLoading({
					title: '加载中'
				})
				uni.request({
					url: `${config.api_base_url}member/address/1/5`,
					header: {
						AUTH: 'ROBOT ' + uni.getStorageSync('token')
					},
					success: res => {
						let {
							status,
							data
						} = res.data;
						if (status.flag === true) {
							this.list = data.rows;
						} else {
							this.list = [];
						}
					},
					fail: () => {
						this.list = [];
					},
					complete: (() => {
						uni.hideLoading()
					})
				});
			},
			onClickDelAddress(id) {
				if (!id) {
					return false;
				}
				uni.showModal({
					content: '确定删除该地址吗？',
					success: res => {
						if (res.confirm) {
							uni.request({
								url: `${config.api_base_url}member/address/${id}`,
								method: 'DELETE',
								header: {
									AUTH: 'ROBOT ' + uni.getStorageSync('token')
								},
								success: res => {
									let {
										status,
										data
									} = res.data;
									if (status.flag === true) {
										uni.showToast({
											icon: 'none',
											title: '删除地址成功'
										});
										this.getAddress();
									} else {
										uni.showToast({
											icon: 'none',
											title: status.msg
										});
									}
								},
								fail: () => {
									uni.showToast({
										icon: 'none',
										title: '删除地址失败, 网络异常'
									});
								}
							});
						}
					}
				});
			},
			onClickGotoAddressAdd() {
				// 新增
				uni.navigateTo({
					url: '/pages/addaddress/addaddress'
				});
			},
			onClickModifyAddress(item) {
				// 修改
				uni.navigateTo({ url: `/pages/addaddress/addaddress?area=${item.area}&city=${item.city}&province=${item.province}&id=${item.id}&recipientName=${item.recipientName}&sex=${item.sex}&phone=${item.phone}&provinceId=${item.provinceId}&cityId=${item.cityId}&areaId=${item.areaId}&address=${item.address}&longitude=${item.longitude}&latitude=${item.latitude}`});
			}
		}
	};
</script>
<style>
	page {
		background-color: #f8f8fa;
	}
</style>
<style scoped>
	.addressmanage {
		width: 100%;
		box-sizing: border-box;
		padding-left: 16rpx;
		padding-right: 14rpx;
		padding-top: 16rpx;
	}

	.addressmanage-list {
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.addressmanage-list-item {
		width: 100%;
		box-sizing: border-box;
		padding-top: 38rpx;
		padding-right: 42rpx;
		padding-bottom: 38rpx;
		padding-left: 38rpx;
		border-radius: 12rpx;
		box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1);
		display: flex;
		align-items: center;
		background-color: #fff;
		margin-bottom: 16rpx;
	}

	.addressmanage-list-item:last-child {
		margin-bottom: 0;
	}

	.addressmanage-list-item-info {
		max-width: 430rpx;
		display: flex;
		align-items: flex-start;
		flex-direction: column;
	}

	.addressmanage-list-item-info-name {
		font-size: 28rpx;
		color: rgba(0, 0, 0, 0.85);
		font-weight: 600;
		line-height: 40rpx;
		margin-bottom: 4rpx;
	}

	.addressmanage-list-item-info-address {
		font-size: 24rpx;
		font-weight: 400;
		color: #6d7278;
		line-height: 34rpx;
	}

	.addressmanage-list-item-handle {
		display: flex;
		align-items: center;
		margin-right: 0;
		margin-left: auto;
	}

	.addressmanage-list-item-handle-mofidy {
		width: 96rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		font-size: 24rpx;
		color: #1677ff;
		border: 2rpx solid #1677ff;
		border-radius: 12rpx;
		margin-right: 16rpx;
	}

	.addressmanage-list-item-handle-del {
		width: 96rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		font-size: 24rpx;
		color: #ff3232;
		border: 2rpx solid #ff3232;
		border-radius: 12rpx;
	}

	.addressmanage-addbtn {
		position: fixed;
		left: 0;
		bottom: 84rpx;
		width: 100%;
		height: 94rpx;
		box-sizing: border-box;
		padding-left: 70rpx;
		padding-right: 70rpx;
	}

	.addressmanage-addbtn-btn {
		width: 100%;
		height: 100%;
		background-color: #f02230;
		border-radius: 12rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 32rpx;
		color: #fff;
		background-color: #f02230;
	}

	.addressmanage-warn {
		width: 100%;
		margin-top: 54rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		line-height: 34rpx;
		color: #aaaaaa;
	}
</style>
